/**
 * Copyright 1998-2006HISEL Group, University of Victoria, Victoria, BCanada.
 * All rights reserved.
 */
package ca.uvic.cs.cogz.mapping.ui.progress;

import java.util.Iterator;
import java.util.LinkedList;


/**
 * This class is used to store the progress of an algorithm's execution.
 * Prompt plugins can use this to provide feedback to Prompt about their progress.
 * This progress is then displayed in the AlgorithmProgressDialog.
 * 
 * @author seanf
 * @date 16-Feb-07
 */
public class ProgressMonitor {
	/** The title of the progress monitor */
	private String progressTitle = "";
	
	/** The current progress of the algorithm */
	private String progressText = "";
	
	/** Indicates the process is completed */
	private boolean complete;
	
	/** Indicates that the process was cancelled */
	private boolean cancelled;
	
	/** List of listener objects that listen to update changes on this object */
	@SuppressWarnings("unchecked")
	private LinkedList updateListeners = new LinkedList();
	
	public ProgressMonitor() {}
	
	public synchronized void setProgressTitle(String progressTitle) {
		this.progressTitle = progressTitle;
		
		fireUpdateProgressEvent();
	}
	
	public synchronized String getProgressTitle() {
		return progressTitle;
	}
	
	public synchronized void setProgressText(String progressText) {
		this.progressText = progressText;
		
		fireUpdateProgressEvent();
	}
	
	public synchronized String getProgressText() {
		return progressText;
	}
	
	public synchronized void setCompleted(boolean complete) {
		this.complete = complete;
		
		fireUpdateProgressEvent();
	}
	
	public synchronized boolean isCompleted() {
		return complete;
	}
	
	public synchronized void setCancelled(boolean cancelled) {
		this.cancelled = cancelled;
		
		fireUpdateProgressEvent();
	}
	
	public boolean isCancelled() {
		return cancelled;
	}
	
	public synchronized void reset() {
		progressText = "";
		progressTitle = "";
		complete = false;
	}
	
	@SuppressWarnings("unchecked")
	public synchronized void addProgressUpdateListener(ProgressUpdateListener l) {
		updateListeners.add(l);
	}
	
	public synchronized void removeProgressUpdateListener(ProgressUpdateListener l) {
		updateListeners.remove(l);
	}
	
	@SuppressWarnings("unchecked")
	private synchronized void fireUpdateProgressEvent() {
		ProgressUpdateEvent updateEvent = new ProgressUpdateEvent(this, this);
		for(Iterator iter = updateListeners.iterator(); iter.hasNext(); ) {
			((ProgressUpdateListener)iter.next()).progressUpdateReceived(updateEvent);
		}
	}
}


